//
//  MPCopyView.m
//  QAPro
//
//  Created by Hyukooooh on 2024/10/10.
//

#import "MPCopyView.h"
@interface MPCopyView ()

@property (nonatomic, copy) NSAttributedString *defaultAttributeString;
@end
@implementation MPCopyView


- (instancetype)init
{
    self = [super init];
    if (self) {
        [self set];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self set];
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    [self set];
}

- (void)set {
    self.canLongTap = true;
//    self.editable = false;
//    self.selectable = false;
    self.userInteractionEnabled = true;
    [self addGestureRecognizer:[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(copyCount:)]];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didHideMenu) name:UIMenuControllerDidHideMenuNotification object:nil];
//    self.scrollEnabled = false;
    //内容缩进为0（去除左右边距）
//    self.textContainer.lineFragmentPadding = 0;
//    文本边距设为0（去除上下边距）
//    self.textContainerInset = UIEdgeInsetsMake(11, 3, 8, 0);
    
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

//复制ID
- (void)copyCount:(UILongPressGestureRecognizer *)longGR {

//    if (self.canLongTap) {
//        if (longGR.state == UIGestureRecognizerStateBegan) {
//            if (![self.text containsString:@"ID"]) return;
//            [self becomeFirstResponder];
//            UIMenuController *menuController = [UIMenuController sharedMenuController];
//            //            menuController.arrowDirection = UIMenuControllerArrowUp;
//            UIMenuItem * copyAAA = [[UIMenuItem alloc] initWithTitle:@"复制" action:@selector(copyA:)];
//            [menuController setMenuItems:@[copyAAA]];
//            [[UIMenuController sharedMenuController] update];
//            
//            //            if (@available(iOS 13.0, *)) {
//            //                [menuController showMenuFromView:self rect:self.bounds];
//            //            } else {
//            [menuController setTargetRect:self.bounds inView:self];
//            [menuController setMenuVisible:YES animated:YES];
//            //            }
//            //            self.selectedRange = NSMakeRange(0, self.text.length);
//            self.defaultAttributeString = self.attributedText;
//            NSAttributedString * at = [[NSAttributedString alloc] initWithString:self.text attributes:@{NSBackgroundColorAttributeName : [UIColor assist_Line_Color], NSForegroundColorAttributeName : self.textColor, NSFontAttributeName : self.font}];
//            self.attributedText = at;
//        }
//    }
}

- (void)copyA:(UIMenuController *)menu {
    UIPasteboard *board = [UIPasteboard generalPasteboard];
    if (self.attributedText.string) {
        board.string = self.attributedText.string;
    } else {
        board.string = self.text;
    }
}

/**
 * 让label有资格成为第一响应者
 */
- (BOOL)canBecomeFirstResponder
{
    return YES;
}

- (void)didHideMenu {
    if (self.defaultAttributeString) {
        self.attributedText = self.defaultAttributeString;
        return;
    }
    if (self.text && self.textColor && self.font) {
        NSAttributedString * at = [[NSAttributedString alloc] initWithString:self.text attributes:@{NSBackgroundColorAttributeName : [UIColor clearColor], NSForegroundColorAttributeName : self.textColor, NSFontAttributeName : self.font}];
        self.attributedText = at;
    }
}

/**
 * label能执行哪些操作(比如copy, paste等等)
 * @return  YES:支持这种操作
 */
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    if (action == @selector(copyA:)) return YES;

    return NO;
}

@end
